const Mock = require('mockjs')

let listData = [
  {
    id: '001',
    date: '2016-05-01',
    name: '王小虎',
    address: '北京市东城区交道口北三条22号院',
  },
  {
    id: '002',
    date: '2022-12-02',
    name: '王芳',
    address: '上海市闵行区龙柏三村156号401室',
  },
  {
    id: '003',
    date: '2023-03-18',
    name: '张华',
    address: '广州市越秀区东风东路707号之一',
  },
  {
    id: '004',
    date: '2023-01-04',
    name: '李梅',
    address: '深圳市福田区益田路皇庭世纪花园',
  },
  {
    id: '005',
    date: '2022-11-01',
    name: '李明',
    address: '广东省深圳市南山区粤海街道科技园南路123号高科技园A区5栋601室',
  },
  {
    id: '006',
    date: '2016-05-06',
    name: '林悠然',
    address: '北京市海淀区中关村街道中关村南大街456号中关村广场B号楼2单元702室',
  },
  {
    id: '007',
    date: '2018-09-07',
    name: '江晨风',
    address: '浙江省杭州市西湖区西溪街道文二路789号西溪花园C幢3单元403室',
  },
  {
    id: '008',
    date: '2023-02-18',
    name: '陈梦琪',
    address: '四川省成都市武侯区簇桥街道武侯大道铁佛段234号和谐家园D栋1单元804号',
  },
  {
    id: '009',
    date: '2021-05-29',
    name: '徐飞扬',
    address: '江苏省南京市秦淮区夫子庙街道健康路567号幸福小区E号楼3室',
  },
  {
    id: '010',
    date: '2018-03-15',
    name: '郭静宜',
    address: '广东省广州市天河区林和街道林和西路345号时代大厦F座1001室',
  },
  {
    id: '011',
    date: '2019-09-11',
    name: '张浩然',
    address: '陕西省西安市雁塔区大雁塔街道小寨西路678号盛世国际G号楼2单元902室',
  },
  {
    id: '012',
    date: '2017-04-12',
    name: '李梓涵',
    address: '福建省福州市鼓楼区东街街道东街901号东方大厦H层503室',
  },
]
Mock.mock('/api/list/list', 'post', (req) => {
  const data = JSON.parse(req.body).body
  return {
    header: {
      code: 0,
    },
    body: {
      count: listData.length,
      list: listData.slice((data.pageNo - 1) * data.pageSize, data.pageNo * data.pageSize),
    },
  }
})

Mock.mock('/api/list/detail', 'post', (req) => {
  const id = JSON.parse(req.body).body.id
  const data = listData.find((item) => item.id === id)
  return {
    header: {
      code: 0,
    },
    body: {
      data,
    },
  }
})

Mock.mock('/api/list/update', 'post', (req) => {
  const data = JSON.parse(req.body).body
  const index = listData.findIndex((item) => item.id === data.id)
  listData[index] = data
  return {
    header: {
      code: 0,
    },
    body: {},
  }
})

Mock.mock('/api/list/del', 'post', (req) => {
  const id = JSON.parse(req.body).body.id
  listData = listData.filter((item) => item.id !== id)
  return {
    header: {
      code: 0,
    },
    body: {},
  }
})
